﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace lcs.Web.Admin.distribution
{
    public partial class listcghs : Web.Admin.BasePage
    {
        Maticsoft.BLL.monthSupplayPlan bll = new Maticsoft.BLL.monthSupplayPlan();
        lcs.BLL.user bu = new BLL.user();
        BLL.configinfo bc = new BLL.configinfo();
        public string dogid = "NO";
        public string Decode(string str)
        {

            if (dogid == "NO")
            {
                if (Request.Cookies["dogid"]!=null)
                    dogid = Request.Cookies["dogid"].Value.ToString();
            }
            return DecodeDog(str, dogid);

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataSet ds_storeInfo = new lcs.BLL.storeinfo().GetList("");
                this.ddl_arrivalAddress.DataTextField = "sname";
                this.ddl_arrivalAddress.DataValueField = "sname";
                this.ddl_arrivalAddress.DataSource = ds_storeInfo;
                this.ddl_arrivalAddress.DataBind();
                //this.ddl_arrivalAddress.Items.Insert(0, new ListItem("---未选择----", "-1"));
                BindData();
                Page.DataBind();
            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }      

        #region gridView

        public void BindData()
        {
            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();            
            if (txtKeyword2.Text.Trim() != "")
            {
                if (Decode("1") == "1")
                strWhere.AppendFormat(" and projectname like '%{0}%'", txtKeyword2.Text.Trim());
            }
            if (txtsname.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and suppliername like '%{0}%'", txtsname.Text.Trim());
            }
            if (ddlstatus.SelectedIndex != 0)
            {
                if (ddlstatus.SelectedIndex == 1)
                    strWhere.Append(" and (arrivalAddress is null or arrivalAddress='')");
                else
                    strWhere.Append(" and (arrivalAddress is not null or arrivalAddress<>'')");
            }
            strWhere.Append(" and status>0 order by suppliername");
            ds = bll.GetListnumshs(strWhere.ToString());
            gridView.DataSource = ds;
            gridView.DataBind();
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }
       
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Attributes.Add("style", "background:#FFF");
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                object obj1 = DataBinder.Eval(e.Row.DataItem, "arrivalAddress");
                if ((obj1 != null && obj1.ToString()!=""))
                {
                    e.Row.Attributes.Add("class", "blue");
                }

            }
        }

        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //#warning 代码生成警告：请检查确认真实主键的名称和类型是否正确
            //int ID = (int)gridView.DataKeys[e.RowIndex].Value;
            //bll.Delete(ID);
            //gridView.OnBind();
        }

        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)gridView.Rows[i].FindControl("DeleteThis");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (gridView.DataKeys[i].Value != null)
                    {
                        idlist += gridView.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }

        #endregion

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0)
                return;
            string arrive = ddl_arrivalAddress.SelectedValue;
            if (bll.UpdatearrivalAddresshs(arrive, idlist))
            {
                BindData();
                Maticsoft.Common.MessageBox.Show(this, "设置成功!");
            }
        }

        protected void btnauto_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0)
                return;
            BLL.distribution dbll=new BLL.distribution();
            if (dbll.UpdateMaterialhs(idlist))
            {
                BindData();
                Maticsoft.Common.MessageBox.Show(this, "设置成功!");
            }
        }
    }
}